import 'package:flutter/material.dart';
import 'package:flutter_kit_plus/flutter_kit.dart';

import '../commont/scan_animation.dart';
import '../tool/assets.dart';
import 'custom_footer.dart';
import 'search_row.dart';

class SearchView extends BLESearchDeviceDialog implements BLESearchDeviceDialogDelegate {
  final GlobalKey<RotateAnimationState> classBKey = GlobalKey<RotateAnimationState>();

  SearchView({
    super.key,
    super.height,
  }) : super() {
    delegate = this;
  }

  @override
  List<String> filterName() => ['LEDDMX'];
  @override
  Color? get contentColor => Color_bg_view;

  @override
  List<RowModel> listModelForHeader(String appVersion) {
    return ['Ungrouped Devices']
        .map(($0) => RowModel(
            text: $0,
            height: 60,
            textStyle: const TextStyle(fontSize: 20, color: Colors.white),
            lineColor: Color_line_view,
            lineHeight: 1,
            bgColor: Colors.transparent))
        .toList();
  }

  @override
  List<RowModel> listModelForSection(List<String> list) {
    return list
        .map(($0) => RowModel(
            text: $0,
            height: 40,
            textStyle: const TextStyle(fontSize: 14, color: Colors.white),
            lineColor: Color_line_view,
            lineHeight: 1,
            bgColor: Colors.transparent,
            iconName: assets.delete,
            iconSize: 20))
        .toList();
  }

  @override
  List<RowModel> listModelForFooter(String appVersion) {
    return (isGroupManager ? [assets.add, assets.back] : ['Group Management', 'Version: $appVersion'])
        .map(($0) => RowModel(
            text: $0,
            textStyle: const TextStyle(fontSize: 14, color: Colors.white),
            height: isGroupManager ? 60 : 40,
            bgColor: Colors.transparent,
            lineColor: Color_line_view,
            lineHeight: 1,
            iconName: $0,
            iconSize: 25))
        .toList();
  }

  @override
  Widget viewForRowInSection(DSPeripheral peripheral, int section, int row, _) {
    return SearchItem(
      device: peripheral,
      isManager: isGroupManager,
      onSelect: () => clickDevice(section, row),
      onPressLongToChangeName: () => changeDeviceName(section, row, peripheral.peripheral, null),
    );
  }

  @override
  void methodForClickBLESearchTableView(BLESearchTableViewClickType type, String tag) {
    if (type == BLESearchTableViewClickType.footer) {
      switch (tag) {
        case 'Group Management':
          setState(() => isGroupManager = true);
        case assets.add:
          var input = InputDialog(
              title: 'New Group',
              contentColor: Color_bg_view,
              textColor: Colors.white,
              lineColor: Color_line_view,
              style: InputDialogStyle.okAndCancel);
          input.show(state.context);
          input.onTap = (tag, content) {
            if (tag == 'OK') {
              addGroup(content);
            }
            print('==============>  修改名称点击  ${tag}');
            return true;
          };
        case assets.back:
          setState(() => isGroupManager = false);
      }
    }
  }

  @override
  void refreshMethodExentesion() => classBKey.currentState?.startAnimation();

  @override
  Widget? bgViewForHeader(Widget child) {
    return Container(
        decoration: const BoxDecoration(gradient: localGradient),
        child: Stack(
          children: [
            Positioned(
                right: 20,
                top: 10,
                bottom: 10,
                width: 25,
                child: ScanAnimation(key: classBKey, child: Image.asset(assets.refresh))),
            Positioned(child: child),
          ],
        ));
  }

  @override
  Widget? bgViewForFooter(Widget child) {
    return Column(
      children: [
        child,
        const CustomFooter(),
      ],
    );
  }
}
